/*
 * @Author: szx
 * @Date: 2023-04-01 15:15:45
 * @LastEditTime: 2023-04-01 16:29:05
 * @Description:
 * @FilePath: \learn\fullstackopen\bloglist-part5\cypress\e2e\blog.cy.js
 */
describe('Blog app', function () {
    beforeEach(function () {
        cy.request('POST', 'http://localhost:3003/api/testing/reset')

        cy.createUser({
            name: 'Matti Luukkainen',
            username: 'mluukkai',
            password: 'salainen'
        })
        cy.createUser({
            name: 'kiki',
            username: '123',
            password: '123'
        })
        cy.visit('http://localhost:3000')
    })

    it('Login form is shown', function () {
        cy.contains('Log in to application')
    })


    describe('Login', function () {
        it('succeeds with correct credentials', function () {
            cy.get('#username').type('mluukkai')
            cy.get('#password').type('salainen')
            cy.get('#login-button').click()

            cy.contains('Matti Luukkainen logged in')
        })

        it('fails with wrong credentials', function () {
            cy.get('#username').type('mluukkai')
            cy.get('#password').type('nimasile')
            cy.get('#login-button').click()


            cy.get('.error')
                .should('contain', 'Wrong credentials')
                .and('have.css', 'color', 'rgb(255, 0, 0)')
                .and('have.css', 'border-style', 'solid')
        })
    })

    describe('when logged in', function () {
        beforeEach(function () {
            cy.login({ username: 'mluukkai', password: 'salainen' })

        })

        it('A blog can be created', function () {
            cy.contains('new Blog').click();
            cy.get('#title').type('巷子深的生活')
            cy.get('#author').type('kiki')
            cy.get('#url').type('https://szx.life')
            cy.contains('create').click()
            cy.contains('巷子深的生活');
        })

        describe('and several notes exist', function () {
            beforeEach(function () {
                cy.createBlog({
                    title: 'First class tests',
                    author: 'Robert C. Martin',
                    url: 'http://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.htmll',
                })
                cy.createBlog({
                    title: '巷子深的生活',
                    author: 'kiki',
                    url: 'https://szx.life',
                })
                cy.createBlog({
                    title: 'Type wars',
                    author: 'Robert C. Martin',
                    url: 'http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html',
                })
            })

            it('check blog can be liked', function () {
                cy.contains('巷子深的生活')
                    .contains('view')
                    .click()

                cy.contains('likes 0').contains('like').click();
                cy.contains('likes 1')
            })
        })
    })

    describe('When logged in', function () {
        beforeEach(function () {
            cy.login({ username: 'mluukkai', password: 'salainen' })
            cy.createBlog({
                title: 'Blog created by mluukkai and cant be deleted by kiki',
                author: 'mluukkai',
                url: 'mluukkai.com'
            })

            cy.login({ username: '123', password: '123' })
            cy.createBlog({
                title: '巷子深的生活',
                author: 'kiki',
                url: 'https://szx.life',
            })
        })

        it('delete blog', function () {
            cy.contains('Blog created by mluukkai and cant be deleted by kiki')
                .find('button')
                .should('contain', 'view')
                .click()
            cy.contains('Blog created by mluukkai and cant be deleted by kiki')
                .parent()
                .parent()
                .find('button')
                .should('not.contain', 'delete')

            cy.contains('巷子深的生活')
                .find('button')
                .should('contain', 'view')
                .click()
            cy.contains('巷子深的生活')
                .parent()
                .parent()
                .find('button')
                .should('contain', 'delete')
            cy.get('#delete').click()

            cy.contains('巷子深的生活').should('not.exist')
        })
    })



    describe('Blogs sorted by number of likes', function () {
        beforeEach(function () {
            cy.login({ username: 'mluukkai', password: 'salainen' })
            cy.createBlog({
                title: 'Post with 1 like',
                author: 'Gustav',
                url: 'gustav.com'
            })
            cy.createBlog({
                title: 'Post with 3 likes',
                author: 'Odd',
                url: 'odd.com'
            })
            cy.createBlog({
                title: 'Post with 2 likes',
                author: 'Hassel',
                url: 'hassel.com'
            })

            cy.contains('Post with 1 like')
                .find('button')
                .should('contain', 'view')
                .click()
            cy.contains('Post with 1 like')
                .parent()
                .find('button')
                .should('contain', 'like')
                .as('1like')

            cy.contains('Post with 2 likes')
                .find('button')
                .should('contain', 'view')
                .click()
            cy.contains('Post with 2 likes')
                .parent()
                .find('button')
                .should('contain', 'like')
                .as('2likes')

            cy.contains('Post with 3 likes')
                .find('button')
                .should('contain', 'view')
                .click()
            cy.contains('Post with 3 likes')
                .parent()
                .find('button')
                .should('contain', 'like')
                .as('3likes')
        })

        it.only('check if blogs are sorted', function () {
            cy.get('@1like').contains('like').as('like1')
            cy.get('@2likes').contains('like').as('like2')
            cy.get('@3likes').contains('like').as('like3')

            cy.get('@like1').click()
            cy.wait(100)
            cy.get('@like2').click()
            cy.wait(100)
            cy.get('@like2').click()
            cy.wait(100)
            cy.get('@like3').click()
            cy.wait(100)
            cy.get('@like3').click()
            cy.wait(100)
            cy.get('@like3').click()
            cy.wait(100)

            cy.get('.blog').then(blogs => {
                expect(blogs[0]).to.contain.text('Post with 3 likes')
                expect(blogs[1]).to.contain.text('Post with 2 likes')
                expect(blogs[2]).to.contain.text('Post with 1 like')
            })
        })
    })


})